package Triangulation.minimalityMetrics;

import datatypes.Edge;
import datatypes.Triangle;
import datatypes.Vertex;
import static tools.Tool.*;

public class ShortestEdgesMetric extends AbstractMinimalityMetric
implements MinimumTriangleSumMetric {

	
	/**
	 * Checks whether edge is minimum
	 * 
	 * 
	 * @param t1 Triangle on one side of the quad
	 * @param t2 Triangle on the other side of the quad
	 * @return true if the edge is locally minimal
	 */
	public boolean edgeIsLocallyMinimal(Edge sharedEdge, Triangle t1, Triangle t2) {
		Vertex trianglePeak1 = t1.getVertexNotOnEdge(sharedEdge);
		Vertex trianglePeak2 = t2.getVertexNotOnEdge(sharedEdge);
		if ( ! quadIsConvex(trianglePeak1, trianglePeak2, sharedEdge)) {
			// If quad is NOT convex, then sharedEdge is locally minimal.
			// See "A Large Subgraph of the Minimum Weight Triangulation" 
			// p.291, 1.3
			return true;
		}
		if (trianglePeak1.distanceSquared(trianglePeak2) < sharedEdge.getLengthSquared()) {
			return false;
		}
		
		return true;
	}
	
	@Override
	public String getName() {
		return "shortest edges";
	}
	
	@Override
	public double getAngleForDiamondProperty() {
		// Diamond property applicable with beta = pi / 4.6
		return Math.PI/(4.6);
	}	

	@Override
	public double getTriangleWeight(Triangle t)
	{
		return t.getEdge1().getLength() + t.getEdge2().getLength() + t.getEdge3().getLength();
	}

	@Override
	public boolean betaSkeletonApplicable() {
		return true;
	}

}
